﻿




//angular.module('IGIS.directives', [])

var app = angular.module('IGIS', ['$strap.directives']);

var url = '/Hetz/api/Polygon/';
app.factory('PolygonFactory', function ($http, $location) {
    return {
        getPolygon: function (id) {
                
            return $http.get(url + '/' + id);
           
        },
        addPolygon: function (customer) {
            return $http.post(url, customer);
        },
        deletePolygon: function (customer) {
            return $http.delete(url + customer.CustomerID);
        },
        updatePolygon: function (polygon) {
            return $http.put(url + polygon.PolygonID, polygon);
        }
    };
});
/*
var searchurl = '/Hetz/api/seachPolygon/';
app.factory('searchPolygonFactory', function ($http, $location) {
    return {
        getPolygon: function (id) {

            return $http.get(searchurl + '/' + id);

        },
        addPolygon: function (customer) {
            return $http.post(searchurl, customer);
        },
        
        searchPolygon: function (polygon) {
            return $http.put(searchurl, polygon);
        }
    };
});*/

var urlSearchConfigurator = '/Hetz/api/SearchConfigurator';
var urlwsSearchConfigurator = '/hetz/IGISWS.svc/GetTablesToSearch';

app.factory('searchPolygonFactory', function ($http, $location) {
    return {
        getall: function () {

            return $http.get(urlSearchConfigurator );

        },
        get: function (id) {

            return $http.get(urlSearchConfigurator + '/' + id);

        },
        add: function (customer) {
            return $http.post(urlSearchConfigurator, customer);
        },

        searchPolygon: function (id,SelectedFilter1, SelectedFilter2, SelectedFilter3,
            SelectedFilter1val, SelectedFilter2val, SelectedFilter3val, SelectedDrpSearchBy) {
            return $http.post(urlwsSearchConfigurator, SelectedFilter1, SelectedFilter2, SelectedFilter3,
            SelectedFilter1val, SelectedFilter2val, SelectedFilter3val, SelectedDrpSearchBy);
        }
    };
});

var urlupload = '/hetz/api/fileupload';

app.directive('datepicker', function ($parse) {
    var directiveDefinitionObject = {
        restrict: 'A',
        link: function postLink(scope, iElement, iAttrs) {
            iElement.datepicker({
                dateFormat: 'yy-mm-dd',
                onSelect: function (dateText, inst) {
                    scope.$apply(function (scope) {
                        $parse(iAttrs.ngModel).assign(scope, dateText);
                    });
                }
            });
        }
    };
    return directiveDefinitionObject;
});
app.factory('uploadManager', function ($http,$rootScope) {
    var _files = [];
    return {
        add: function (file) {
            _files.push(file);
            $rootScope.$broadcast('fileAdded', file.files[0].name);
        },
        clear: function () {
            _files = [];
        },
        files: function () {
            var fileNames = [];
            $.each(_files, function (index, file) {
                fileNames.push(file.files[0].name);
            });
            return fileNames;
        },
        upload: function () {
           
            var returnval = "";
            var returnval2= "";
            
            var files = $("#file1").get(0).files;
            if (_files.length > 0) {
                if (window.FormData !== undefined) {
                    var data = new FormData();
                    for (i = 0; i < _files.length; i++) {
                        data.append("file" + i, _files[i]);
                    }

                    
                    returnval = $http.post(urlupload, data);
                    $.ajax({
                        type: "POST",
                        url: urlupload,
                        contentType: false,
                        processData: false,
                        data: data,
                        success: function (results) {
                            for (i = 0; i < results.length; i++) {
                                //alert("Successfully uploaded " + results[i].Name + " (" + results[i].Size + " bytes)");
                                returnval += results[i];
                            }
                        }
                    });
                }
            }
            return returnval;
            this.clear();
            
        },
        setProgress: function (percentage) {
            $rootScope.$broadcast('uploadProgress', percentage);
        }
    };
});

app.directive('upload', ['uploadManager', function factory(uploadManager) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            $(element).fileupload({
                dataType: 'text',
                add: function (e, data) {
                    uploadManager.add(data);
                },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    uploadManager.setProgress(progress);
                },
                done: function (e, data) {
                    uploadManager.setProgress(0);
                }
            });
        }
    };
}]);

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


